﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XProject.DataVisit.Model.Advertisement;
using NHibernate;
using NHibernate.Criterion;
using NHibernate.Transform;
using System.Collections;
using XProject.DataVisit.BLL;

namespace XProject.DataVisit.DAL.Advertisement
{
    public class DBindAdvertisement : AbstractSupportDal<BindAdvertisement>
    {
        /// <summary>
        /// 根据设备ID
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public IList<BindAdvertisement> GetListByEKey(Guid ID)
        {
            IList<BindAdvertisement> t = new List<BindAdvertisement>();
            Template.Execute(delegate(ISession session)
            {
                t = session.CreateCriteria<BindAdvertisement>()
                    .Add(Expression.Eq("EquipmentID", ID))
                    .List<BindAdvertisement>();
                return t;
            });
            return t;
        }

        #region 返回网关当前时间需要的广告URL
        /// <summary>
        /// 返回网关根据时间段解析的广告URL地址
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public GatewayAdvertisement GetAdvertisementUrl(string ID)
        {
            var temp = Session.GetNamedQuery("GetAdvertisement_Equipment")
                .SetDateTime("ctime", DateTime.Now)
                .SetString("code", ID)
                .SetResultTransformer(Transformers.AliasToEntityMap)
                .UniqueResult();
            return GetObject((Hashtable)temp);
        }

        private GatewayAdvertisement GetObject(Hashtable k)
        {
            IDictionary<string, object> param = new Dictionary<string, object>();
            param=PublicFn.GetHashValue(k);
            GatewayAdvertisement g = new GatewayAdvertisement();
            g.Url = param["URL"].ToString();
            return g;
        }
        #endregion
    }
}
